package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.mapper.ManufactureApplyMapper;
import com.guigu.erp.pojo.ManufactureApply;
import com.guigu.erp.service.ManufactureApplyService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.ManufactureApplyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ManufactureApplyServiceImpl extends ServiceImpl<ManufactureApplyMapper,ManufactureApply>
    implements ManufactureApplyService {
    @Autowired
    private ManufactureApplyMapper manufactureApplyMapper;
    @Override
    public String getMaxDesignId() {
        ManufactureApply manufactureApply = manufactureApplyMapper.queryMaxModule();
        if (manufactureApply == null){
            return null;
        }
        String designId = manufactureApply.getApplyId();
        return designId;
    }

    @Override
    public PageUtils<ManufactureApply> getAllApply(ManufactureApplyVo vo, Integer pageNo, Integer pageSize) {
        QueryWrapper<ManufactureApply> wrapper = new QueryWrapper<>();
        if (vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_id())){
                wrapper.like("APPLY_ID","%"+vo.getSearch_id()+"%");
            }
            if(!StringUtils.isEmpty(vo.getSearch_name())){
                wrapper.like("PRODUCT_NAME","%"+vo.getSearch_name()+"%");
            }
            if(!StringUtils.isEmpty(vo.getSearch_state())){
                wrapper.eq("CHECK_TAG",vo.getSearch_state());
            }
            if(!StringUtils.isEmpty(vo.getSearch_time1())){
                wrapper.ge("REGISTER_TIME",vo.getSearch_time1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_time2())){
                wrapper.le("REGISTER_TIME",vo.getSearch_time2());
            }
        }
        wrapper.eq("MANUFACTURE_TAG","P001-0");
        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureApply> list = manufactureApplyMapper.selectList(wrapper);
        PageInfo<ManufactureApply> pageInfo = new PageInfo<>(list);
        PageUtils<ManufactureApply> pageUtils =new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    @Override
    public PageUtils<ManufactureApply> queryUnMakeApply(ManufactureApplyVo vo, Integer pageNo, Integer pageSize) {
        QueryWrapper<ManufactureApply> wrapper = new QueryWrapper<>();
        if (vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_id())){
                wrapper.like("APPLY_ID","%"+vo.getSearch_id()+"%");
            }
            if(!StringUtils.isEmpty(vo.getSearch_name())){
                wrapper.like("PRODUCT_NAME","%"+vo.getSearch_name()+"%");
            }
            if(!StringUtils.isEmpty(vo.getSearch_time1())){
                wrapper.ge("CHECK_TIME",vo.getSearch_time1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_time2())){
                wrapper.le("CHECK_TIME",vo.getSearch_time2());
            }
        }
        wrapper.eq("CHECK_TAG","S001-1");
        wrapper.eq("MANUFACTURE_TAG","P001-0");
        PageHelper.startPage(pageNo,pageSize);

        List<ManufactureApply> list = manufactureApplyMapper.selectList(wrapper);
        PageInfo<ManufactureApply> pageInfo = new PageInfo<>(list);
        PageUtils<ManufactureApply> pageUtils =new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }
}
